﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PM.Entity;
using PM.Services;
using System.Collections.Generic;

namespace PM.Site
{
    public partial class ClientsOverview : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null || Session["UserTypeId"] == null)
            {
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
            else if (Session["UserTypeId"] != null)
            {
                if (Int32.Parse(Session["UserTypeId"].ToString()) == 1)
                {
                    this.Loading();
                }
                else
                {
                    Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
                }

            }
            else
            {
                Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
            }          

        }

        private void Loading()
        {
            if (!Page.IsPostBack)
            {
                ClientCollection client_col = ClientServices.GetClientCollection();
                Session["ClientList"] = client_col;
                dlClients.DataSource = client_col;
                dlClients.DataBind();         

            }
        }

        protected void dlClients_ItemCommand(object source, DataListCommandEventArgs e)
        {
            string clientId = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "EditClient":
                    Session["ClientId"] = clientId;
                    Response.Redirect("~/ClientForm.aspx");
                    break;               
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (Session["ClientList"] == null)
            {
                return;
            }
            List<Client> Original_lstClients = ((ClientCollection)Session["ClientList"]).ToList<Client>();
            ClientCollection updated_list = new ClientCollection();
            bool update = false;
            foreach (DataListItem item in dlClients.Items)
            {
                string clientId = dlClients.DataKeys[item.ItemIndex].ToString();
                Client client = new Client(clientId);
                Predicate<Client> findMatch = new Predicate<Client>(client.MatchFound);
                client = Original_lstClients.Find(findMatch);
                CheckBox chkbActive = (CheckBox)item.FindControl("chkActive");
                if (client.Active != chkbActive.Checked)
                {
                    client.Active = chkbActive.Checked;
                    updated_list.Add(client);
                    update = true;

                }
            }
            Session["ClientList"] = null;
            if (update)
            {
                ClientServices.UpdateCollection(updated_list);
                Response.Redirect("~/ClientsOverview.aspx");
            }

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Session["ClientId"] = null;
            Response.Redirect("~/ClientForm.aspx");
        }
    }
}
